#include "WorkWithMain.h"
#include "CenteralWidget.h"
#include "UIUtils.h"
#include <QSystemTrayIcon>
#include <QMenu>
#include <QEvent>
#include <QWindowStateChangeEvent>
#include <QTimer>
#include <QWinTaskbarProgress>
#include <QWinTaskbarButton>

const QString kTitleName = "WorkWith";

class MainWindow::Impl
{
public:
    CentralWidget centWidget;
    QSystemTrayIcon *minTray = nullptr;
    QWinTaskbarButton *windowsTaskbarButton;
    QWinTaskbarProgress *windowsTaskbarProgress;
    QPixmap pixmap;
    bool closeWindow = false;

    void initTrayMenu(MainWindow *);
};

void MainWindow::Impl::initTrayMenu(MainWindow *mainWdow)
{
    QMenu *menu = new QMenu;
    QAction *pExit = new QAction("Exit", minTray);
    menu->addAction(pExit);
    minTray->setContextMenu(menu);
    minTray->show();
    minTray->setVisible(true);
    QObject::connect(pExit, &QAction::triggered, [ &, mainWdow]() {
        minTray->deleteLater();
        minTray->setVisible(false);
        closeWindow = true;
        mainWdow->close();
    });
    QObject::connect(minTray, &QSystemTrayIcon::activated, [ &,
    mainWdow](QSystemTrayIcon::ActivationReason reason) {
        if (reason == QSystemTrayIcon::DoubleClick)
            mainWdow->setVisible(true);
    });
}

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), _impl(new Impl())
{
    setFixedSize(QSize(1000, 1010));
    setCentralWidget(&_impl->centWidget);

    setWindowTitle(kTitleName);

    if (_impl->pixmap.load(QString(":/icons/mainIcon.png")))
        setWindowIcon(QIcon(_impl->pixmap));

    _impl->minTray = new QSystemTrayIcon(QIcon(_impl->pixmap), this);
    _impl->minTray->setToolTip(kTitleName);
    _impl->initTrayMenu(this);
    if (Message::TrayMessage *pTrayMsg = Message::TrayMessage::Ins()) {
        pTrayMsg->registTray(_impl->minTray);
        pTrayMsg->registMainWindow(this);
        QObject::connect(pTrayMsg, &Message::TrayMessage::startTaskBarProgress, this,
                         &MainWindow::startTaskBarProgress);
        QObject::connect(pTrayMsg, &Message::TrayMessage::setTaskBarProgressValue, this,
                         &MainWindow::setTaskBarValue);
    }
}

MainWindow::~MainWindow()
{

}

void MainWindow::showTrayMsg(const QString &msg)
{
    _impl->minTray->showMessage("msg", msg, QIcon(_impl->pixmap));
}
void MainWindow::changeEvent(QEvent *event)
{
    QMainWindow::changeEvent(event);
    if (event->type() != QEvent::WindowStateChange) return;
//     if (QWindowStateChangeEvent* pStateEvent = dynamic_cast<QWindowStateChangeEvent*>(event)) {
//      Qt::WindowStates oldState = pStateEvent->oldState();
//         if (Qt::WindowMinimized == oldState) {
//             _impl->minTray->setVisible(false);
//         }
//         else if (Qt::WindowMaximized == oldState) {
//             _impl->minTray->setVisible(true);
//         }
//     }

}

void MainWindow::closeEvent(QCloseEvent *event)
{
    if (_impl->closeWindow) QMainWindow::closeEvent(event);
    setVisible(false);
    event->ignore();
}
void MainWindow::startTaskBarProgress(int minVal, int maxVal)
{
    if (!_impl->windowsTaskbarButton) {
        _impl->windowsTaskbarButton = new QWinTaskbarButton(this);
        _impl->windowsTaskbarButton->setWindow(windowHandle());
        _impl->windowsTaskbarProgress = _impl->windowsTaskbarButton->progress();
    }
    _impl->windowsTaskbarProgress->setRange(minVal, maxVal);
    _impl->windowsTaskbarProgress->setValue(0);
    _impl->windowsTaskbarProgress->show();
}
void MainWindow::setTaskBarValue(int val)
{
    _impl->windowsTaskbarProgress->setValue(val);
}
